package org.leo.dm.example.flow;

import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;

import org.leo.dm.common.bean.ResultBean;
import org.leo.dm.common.bean.StepResultBean;
import org.leo.dm.component.engine.DefaultStorage;
import org.leo.dm.component.flow.Flow;
import org.leo.dm.component.reader.db.DbReader;
import org.leo.dm.component.util.DbBean;
import org.leo.dm.component.util.DbManager;
import org.leo.dm.component.util.DbTypeEnum;
import org.leo.dm.example.base.FlowDaemon;
import org.leo.dm.example.callback.CallBackExample;
import org.leo.dm.example.plugin.PluginColumnCustom;
import org.leo.dm.example.plugin.PluginSegmentCustom;
import org.leo.dm.example.writer.SaxXmlWriter;
import org.leo.dm.util.ThreadPoolUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FlowDbToXmlExample extends Flow {
	Logger log = LoggerFactory.getLogger(this.getClass());

	@Override
	public ResultBean handle(Map<String, Object> param) {
		ResultBean resultBean = new ResultBean();
		try {
			Connection conn = null;
			DbBean dbBean = null;

			dbBean = new DbBean();
			dbBean.setIp("192.168.147.127");
			dbBean.setPort("3306");
			dbBean.setInstance("di_ehr_310000001");
			dbBean.setUser("root");
			dbBean.setPassword("123456");
			dbBean.setType(DbTypeEnum.MYSQL);
			conn = DbManager.getConection(dbBean);

			DbReader reader = new DbReader();
			reader.setConn(conn);
			reader.setSourceTableNames(new String[] { "di_testtablethree_bak", "di_testtabletwo_bak", "di_testtableone_bak" });
			reader.setSegmentPlugin(new PluginSegmentCustom());
			reader.setColumnPlugin(new PluginColumnCustom());
			reader.setSqls(new String[] { "select * from di_testtablethree_bak", "select * from di_testtabletwo_bak", "select * from di_testtableone_bak" });
			reader.setCallBack(new CallBackExample());

			// 生成XML 填充的管理字段内容
			Map<String, String> xmlMap = new HashMap<String, String>();
			xmlMap.put("sourcedomain", "310000001");
			xmlMap.put("businesstype", "firsttest");
			xmlMap.put("taskid", "1");
			xmlMap.put("callbackdomainurl", "N001");
			xmlMap.put("callbackdomainfun", "N001");
			xmlMap.put("callbackdomainns", "N001");

			SaxXmlWriter writer = new SaxXmlWriter();
			writer.setXmlMap(xmlMap);
			writer.setDestPath("d:/shk_1116_1655.xml");
			writer.setDestZipPath("d:/shk_1116_1655z.xml");
			writer.setCallback(new CallBackExample());

			DefaultStorage storage = new DefaultStorage(20000);
			storage.setReader(reader);
			storage.setWriter(writer);

			this.setReader(reader);
			this.setWriter(writer);
			this.setStorage(storage);

			this.run();

			// 监控数据读写效率
			new FlowDaemon().handle(new ThreadPoolUtil(), reader, writer);

			// 同步等待返回值
			StepResultBean readerResultBean = reader.getResultBean();
			log.info("readerResultBean" + readerResultBean.toStr());
			StepResultBean writerResultBean = writer.getResultBean();
			log.info("writerResultBean" + writerResultBean.toStr());

			if ("-1".equals(readerResultBean.getCode()) || "-1".equals(writerResultBean.getCode())) {
				resultBean.setCode("-1");
			} else {
				resultBean.setCode("1");
			}
		} catch (Exception e) {
			e.printStackTrace();
			resultBean.setCode("-1");
		}
		return resultBean;
	}

	public static void main(String[] args) {
		Map<String, Object> param = new HashMap<String, Object>();
		FlowDbToXmlExample flow = new FlowDbToXmlExample();
		flow.handle(param);
	}
}
